package com.feng.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import com.alibaba.fastjson.JSON;
import com.feng.dao.UserDao;
import com.feng.entity.User;
import com.feng.entity.UserConfig;
import com.feng.service.IUserConfigService;
import com.feng.service.IUserService;
import com.feng.util.CommonUtils;
import com.feng.util.CookieUtils;
import com.feng.util.Dict;
import com.feng.util.RedisUtils;

@Service
public class UserServiceImpl implements IUserService {

	@Resource
	private UserDao userDao;
	@Autowired
	private IUserConfigService userConfigService;
	@Autowired
	private RedisUtils redisUtils;
	@Override
	public List<User> selectAllUsers() {
		return userDao.selectAllUsers();
	}
	public List<User> selectAllByPage(@PathVariable int pageNum) {
		List<User> userList=userDao.selectAllUsers();
		return userList;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public User findUser(String name, String upwd) throws Exception {
		User user = new User();
		user.setName(name);
		user.setUpwd(upwd);
		return userDao.findUser(user);
	}
	@Override
	public Integer insertUser(User user) throws Exception {
		user.setId(CommonUtils.getUUID());
		return userDao.insertUser(user);
	}
	@Override
	public boolean ckUserInvalid(HttpServletRequest request) throws Exception {
		String userCookie = CookieUtils.getUserCookie(request,Dict.USER_LOGIN_COOKIE);
		if(CommonUtils.isEmpty(userCookie)) {//未登录
			return true;
		}else {
			if("0".equals(userConfigService.ckUserSync(request))) {
				String userInfo = redisUtils.getString(userCookie);
				if(CommonUtils.isNotEmpty(userInfo)){
					//与redis中的cookieid进行比较
					User user = JSON.parseObject(userInfo, User.class);
					String cookieId = redisUtils.getString(user.getId());//获取最新的cookieid
					if(!userCookie.equals(cookieId)) {
						return false;//已登录且失效
					}
					
				}
			}
			
		}
		
		return true;
	}
	@Override
	public User getCurrentUser(HttpServletRequest request) {
		String userCookie = CookieUtils.getUserCookie(request,Dict.USER_LOGIN_COOKIE);
		if(CommonUtils.isNotEmpty(userCookie)) {
			String userInfo = redisUtils.getString(userCookie);
			return JSON.parseObject(userInfo, User.class);
		}
		return null;
	}
	@Override
	public void setUserSync(HttpServletRequest request,String sync) throws Exception {
		User user = this.getCurrentUser(request);
		if(user != null) {
			UserConfig userConfig = userConfigService.findByUid(user.getId());
			if(userConfig == null) {
				userConfig = new UserConfig();
				userConfig.setUid(user.getId());
				userConfig.setSynchro(sync);
				userConfigService.insertUserConfig(userConfig);
			}else {
				userConfig.setCredate(new Date());
				userConfig.setSynchro(sync);
				userConfigService.updateIsSynchro(userConfig);
			}
			
		}
		
		
	}
}
